package com.sufen.seckill.rabbitmq;

import com.sufen.seckill.pojo.Order;
import com.sufen.seckill.pojo.SeckillMessage;
import com.sufen.seckill.pojo.SeckillOrder;
import com.sufen.seckill.pojo.User;
import com.sufen.seckill.service.IGoodsService;
import com.sufen.seckill.service.IOrderService;
import com.sufen.seckill.utils.JsonUtil;
import com.sufen.seckill.vo.GoodsVo;
import com.sufen.seckill.vo.RespBean;
import com.sufen.seckill.vo.RespBeanEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author sufen
 * @Date 2021/10/28 13:55
 * @Version 1.0
 * 功能描述：
 */
@Service
@Slf4j
public class MQReceiver {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IOrderService orderService;

    private Map<Long,Boolean> EmptyStockMap=new HashMap<>();



    @Autowired
    private IGoodsService goodsService;

//    下单操作
    @RabbitListener(queues = "seckillQueue")
    public void receive(String message){
        log.info("接收到的消息："+message);
        SeckillMessage seckillMessage = JsonUtil.jsonStr2Object(message, SeckillMessage.class);
        Long goodId=seckillMessage.getGoodId();
        User user=seckillMessage.getUser();
        GoodsVo goodsVo=goodsService.findGoodsVoByGoodsId(goodId);
        if(goodsVo.getStockCount()<1){
            return;
        }
        //判断重复抢购，用户ID和商品ID
        SeckillOrder seckillOrder=(SeckillOrder) redisTemplate.opsForValue().get("order:"+user.getId()+":"+goodId);
        if(seckillOrder!=null){
            return;
        }
        //下单操作
        Order order=orderService.seckill(user,goodsVo);
    }

//    @RabbitListener(queues = "queue")
//    public void receive(Object msg){
//        log.info("接收消息"+msg);
//    }
//
//    @RabbitListener(queues = "queue_fanout01")
//    public void receive01(Object msg){
//        log.info("QUEUE01接收消息"+msg);
//    }
//
//    @RabbitListener(queues = "queue_fanout02")
//    public void receive02(Object msg){
//        log.info("QUEUE02接收消息"+msg);
//    }
//
//    @RabbitListener(queues = "queue_direct01")
//    public void receive03(Object msg){
//        log.info("queue_direct01接收消息"+msg);
//    }
//
//    @RabbitListener(queues = "queue_direct02")
//    public void receive04(Object msg){
//        log.info("queue_direct02接收消息"+msg);
//    }
//
//    @RabbitListener(queues = "queue_topic01")
//    public void receive05(Object msg){
//        log.info("queue_topic01接收消息"+msg);
//    }
//
//    @RabbitListener(queues = "queue_topic02")
//    public void receive06(Object msg){
//        log.info("queue_topic02接收消息"+msg);
//    }
}
